package org.jtb.quickr;

import java.io.InputStream;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.apache.log4j.Logger;
import org.jdom.Element;

/**
 * Contexts response.
 *
 * @see org.jtb.quickr.PhotosService#getAllContexts(String)
 */
public class ContextsResponse extends FlickrResponse {
    /**
     * Set response.
     */
    public static class PhotoSet extends FlickrResponse {
        private String id;
        private String title;
        
        PhotoSet(Element e) throws QuickrException {
            super(e);
        }
        
        protected void populate(Element e) {
            this.id = e.getAttributeValue("id");
            this.title = e.getAttributeValue("title");
        }        

        public String getId() {
            return id;
        }

        public String getTitle() {
            return title;
        }
    }

    /**
     * Pool response.
     */
    public static class PhotoPool extends FlickrResponse {
        private String id;
        private String title;
        
        PhotoPool(Element e) throws QuickrException {
            super(e);
        }
        
        protected void populate(Element e) {
            this.id = e.getAttributeValue("id");
            this.title = e.getAttributeValue("title");
        }        

        public String getId() {
            return id;
        }

        public String getTitle() {
            return title;
        }
    }
    
    
    private static Logger logger = Logger.getLogger(ContextsResponse.class.getName());
    
    private Set<PhotoSet> sets;
    private Set<PhotoPool> pools;
    
    ContextsResponse(InputStream is) throws QuickrException {
        init(is);
    }
    
    protected void populate(Element e) throws QuickrException {
        sets = new HashSet<PhotoSet>();
        List<Element> setElements = e.getChildren("set");
        if (logger.isDebugEnabled()) {
            logger.debug("number of sets: " + setElements.size());
        }
        for (Element setElement: setElements) {
            PhotoSet ps = new PhotoSet(setElement);
            sets.add(ps);
        }

        pools = new HashSet<PhotoPool>();
        List<Element> poolElements = e.getChildren("pool");
        if (logger.isDebugEnabled()) {
            logger.debug("number of pools: " + poolElements.size());
        }
        for (Element poolElement: poolElements) {
            PhotoPool pp = new PhotoPool(poolElement);
            pools.add(pp);
        }
    }    

    public Set<PhotoSet> getSets() {
        return Collections.unmodifiableSet(sets);
    }

    public Set<PhotoPool> getPools() {
        return Collections.unmodifiableSet(pools);
    }
}
